import pdfkit, time, pprint
from selenium import webdriver
 
options_chrome = webdriver.ChromeOptions()
# 以最高权限运行
#options_chrome.add_argument('--no-sandbox')
# 浏览器不提供可视化页面，linux下如果系统不支持可视化不加这条会启动失败
#options_chrome.add_argument('--headless')
# executable_path为chromedriver的位置
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.by import By
#chrome_options.add_argument("--no-sandbox")#root权限下运行所以禁用沙盒
chrome_options.add_argument("--disable-dev-shm-usage")#关闭开发检测，不然会报错
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://learning.oreilly.com/library/view/domain-specific-languages/9780132107549/ch01.html")
print(driver.title)
# 浏览器全屏
driver.fullscreen_window()

# sleep 1秒
time.sleep(1)
source_text = driver.page_source
 
options_pdf = {
    'page-size': 'A4'
}
result = pdfkit.from_string(source_text, 'test.pdf', options=options_pdf)
time.sleep(100)
#driver.quit()